// pages/earnCoin/earnCoin.js
var network = require("../../utils/NetWorkUtils.js")
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    signDisabled: false,//是否禁用按钮
    signColor: "white",//按钮颜色
    signBackground: "#ff4849",//按钮背景颜色
    sign_get_gold: "+0",//签到按钮文字
    bindDisabled: false,//是否禁用按钮
    bindColor: "white",//按钮颜色
    bindBackground: "#ff4849",//按钮背景颜色
    bindBtnText: "+10",//绑定小程序按钮文字
    img:'',//底部banner
    invite_get_gold:"+0",
    sign_Coin:0,
    jump_object:'',//底部banner跳转url
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      sign_get_gold: "+" + options.signGetGold,
      invite_get_gold: options.invite_get_gold,
      sign_Coin: options.signGetGold
    })
    this.requestIsState()
    this.requestButtomBanner()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (option) {
    if (option.from == 'button') {
      wx.showShareMenu({
        withShareTicket: true
      })
      return {
        title: '我正在答题赚钱，免费兑换商品，快来和我一起抢！',
        path: 'pages/index/index?openid=' + app.globalData.openId,
        imageUrl: '../../images/share_pic.jpg',
      }
    } else {
      wx.showShareMenu({
        withShareTicket: true
      })
      return {
        title: '我正在答题赚钱，免费兑换商品，快来和我一起抢！',
        path: 'pages/index/index',
        imageUrl: '../../images/share_pic.jpg',
      }
    }
  },

  _signShareButtonBeTouch: function () {
    this.alertSignInview = this.selectComponent("#alertSignInview");
    this.alertSignInview.hideAlertView();
  },
  //签到按钮点击事件
  signClick: function () {
    this.requestSigned();
  },


  //添加到我的小程序
  // addClick: function () {
  //   var that = this
  //   var dataParam = {}
  //   network.requestHttp("m=api&c=Index&a=addmini", dataParam, function (res) {
  //     console.log(res.data.msg)
  //     if(res.data.msg=='ok'){
  //       that.setData({
  //         bindDisabled: true,//是否禁用按钮
  //         bindColor: "#999999",//按钮颜色
  //         bindBackground: "#ececec",//按钮背景颜色
  //         bindBtnText: "已完成",//签到按钮文字
  //       })
  //     }else{
  //       wx.showToast({
  //         title: res.data.msg ,
  //         icon: 'none',
  //         duration: 2000
  //       })
  //     }
  //   })
  // },

  //是否签到,是否绑定小程序
  requestIsState: function () {
    var that = this
    var dataParam = {}
    network.requestHttp("m=api&c=Index&a=task", dataParam, function (res) {
      console.log(res)
      if (res.data.data.sign == 0) {//0为已签到
        that.setData({
          signDisabled: true,//是否禁用按钮
          signColor: "#999999",//按钮颜色
          signBackground: "#ececec",//按钮背景颜色
          sign_get_gold: "已签到",
        })
      }else{
        // that.setData({
        //   signDisabled: false,//是否禁用按钮
        //   signColor: "#ff4849",//按钮颜色
        //   signBackground: "#ffffff",//按钮背景颜色
        //   sign_get_gold: "+"+sign_get_gold,
        // }) 
      }
      console.log(res.data.data.add)
      if (res.data.data.add == 0) {
        that.setData({
          bindDisabled: true,//是否禁用按钮
          bindColor: "#999999",//按钮颜色
          bindBackground: "#ececec",//按钮背景颜色
          bindBtnText: "已完成",//签到按钮文字
        })
      }else
      {
        var coin = '+' + res.data.data.add_coin
          that.setData({
            bindBtnText: coin,//签到按钮文字
          })

      }
    })
  },
  //签到接口
  requestSigned: function () {
    var that = this;
    var dataParam = {}
    network.requestHttp("m=api&c=Index&a=sign", dataParam, function (res) {
      console.log(res)
      if (res.data.state == 1) {

        that.alertSignInview = that.selectComponent("#alertSignInview");
        that.alertSignInview.showAlertView();

        that.setData({
          signDisabled: true,//是否禁用按钮
          signColor: "#999999",//按钮颜色
          signBackground: "#ececec",//按钮背景颜色
          sign_get_gold: "已签到"
        })
      }
    })
  },

  //请求底部banner图片
  requestButtomBanner: function () {
    var that = this;
    var requestData = { position: 4 }
    network.requestHttp("m=api&c=Index&a=banner", requestData, function (res) {
      console.log(res)
      that.setData({
        img: res.data.data.pic,
        jump_object: res.data.data
      })
    })
  },
  //底部banner跳转
  banner_jump() {
    var dataParam = {
      id: 4
    }
    network.requestHttp("m=api&c=Index&a=clicked", dataParam, function (res) {

    })
    var that = this;
    if (that.data.jump_object.jump == '2') {
      wx.navigateToMiniProgram({
        appId: that.data.jump_object.appid,
        path: that.data.jump_object.path
      })
    } else {
      wx.navigateTo({
        url: '../webView/indexWebView?bannerUrl=' + that.data.jump_object.jump_url
      })
    }
  },

})